/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-14
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "sys_task.h"
#include "hal_gpio.h"
#include "app_buttons.h"
#include "sw_timer_task.h"
#include "app_global_data.h"

#if (BUTTON_NUM > 0)

/*----------------------------macro file---------------------------*/
#define APP_BTN_LOG_ENABLE	1

#if APP_BTN_LOG_ENABLE
#include "log.h"
#define APP_BTN_LOG			LOG_DEBUG
#else
#define APP_BTN_LOG(...)
#endif


#define BUTTON_DETECT_INTERVAL_MS		50
#define DOUBLE_CLICK_VALID_MS			500

#define BTN_SHORT_PRESS_CNT				1
#define BTN_LONG_PRESS_CNT				80	 //4s
#define BTN_SN_DISP_PRESS_CNT			160  //8s
#define BTN_RST_PRESS_CNT				200  //10s

#define BTN_COUPLE_CLICK_CNT    2  //Press 2 times continuously
#define BTN_SWITCH_CLICK_CNT    5  //Press 5 times continuously
#define BTN_SN_CLICK_CNT        10  //Press 10 times continuously
#define BTN_RST_CLICK_CNT       15 //Press 15 times continuously

#define BUTTON_DETECT_TIMER_START()     button_timer_start(BUTTON_DETECT_INTERVAL_MS)

#define BUTTON_CLICK_TIMER_START()      couple_click_timer_start(DOUBLE_CLICK_VALID_MS)

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/
static bool btn_is_pressed = false; //Prevent start timer repeatly.
static uint8_t couple_click_cnt = 0;
static uint16_t btn_press_det_cnt;

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

void app_button_click_timeout_handle(void)
{
    couple_click_cnt = 0;
}

void app_button_timeout_handle(void)
{
	if(0 == hal_gpio_read(BUTTON1_PIN_NUM)) {
		if(btn_press_det_cnt < BTN_RST_PRESS_CNT) {
			btn_press_det_cnt++;
			if(BTN_LONG_PRESS_CNT == btn_press_det_cnt) {
				////////sys_task_evt_set(SYS_EVT_LONG_PRESS);
			}
#if(DEV_MAC_BURN_ENABLE)
			else if(BTN_SN_DISP_PRESS_CNT == btn_press_det_cnt) {
				/////////sys_task_evt_set(SYS_EVT_SN_DISP_PRESS);
			}
#endif
			BUTTON_DETECT_TIMER_START();
		}
		else {
			//sys_task_evt_set(SYS_EVT_SHIP_MODE_RESET);
			btn_is_pressed = false;
		}
	}
	else { //Release
		btn_is_pressed = false;
        if(btn_press_det_cnt > 0) {
            couple_click_cnt++;
            if(1 == couple_click_cnt) {
                sys_task_evt_set(SYS_EVT_SHORT_PRESS);
            }
            else if(couple_click_cnt == BTN_COUPLE_CLICK_CNT) {
                sys_task_evt_set(SYS_EVT_DOUBLE_CLICK);
            }
            else if(couple_click_cnt == BTN_SWITCH_CLICK_CNT) {
                sys_task_evt_set(SYS_EVT_LONG_PRESS);
            }
            else if(couple_click_cnt == BTN_SN_CLICK_CNT) {
                sys_task_evt_set(SYS_EVT_SN_DISP_PRESS);
            }
            else if(couple_click_cnt == BTN_RST_CLICK_CNT) {
                sys_task_evt_set(SYS_EVT_SHIP_MODE_RESET);
            }
            BUTTON_CLICK_TIMER_START();
		}
	}
}

void app_button_int_cb(uint8_t pin_num)
{
	if(DEV_MODE_DEEP_SLEEP == g_app_data.sys.mode) {
		app_platform_reset();
	}

	if(false == btn_is_pressed) {
		btn_press_det_cnt = 0;
		BUTTON_DETECT_TIMER_START();
		btn_is_pressed = true;
	}
}

void app_buttons_init(void)
{
	hal_gpio_input_cfg(BUTTON1_PIN_NUM, PULL_UP);
	hal_gpio_int_enable(BUTTON1_PIN_NUM, FALLING_EDGE, app_button_int_cb);
}


#endif	//(BUTTON_NUM > 0)
